﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.ComponentModel;
using ShopB2C.ViewModels;
using ShopB2C.Enums;
using ShopB2C.Entity;

namespace xTeam.HengLe.Controller.Website
{
    public partial class CustomerController
    {
        private string _orderNo = "{0}";
        /// <summary>
        /// 增加到订单
        /// </summary>
        public ActionResult AddToOrder(
             [DefaultValue(0)]int addressId)
        {
            if (addressId<=0)
            {
                return RedirectToAction("OrderPreview");
            }
            string orderId = string.Format(_orderNo, DateTime.Now.ToString("yyyyMMddHHssmmfff"));
            int result = _orderAccess.AddToOrder(orderId, addressId, _user.Uid, (int)ShopCarType.ShopCar);
            return RedirectToAction("OrderList");
        }

        /// <summary>
        /// 修改购物车产品信息
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="count"></param>
        /// <param name="property"></param>
        /// <param name="Description"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateCart(string productId,string count,string property,string description) { 
            int uid=_user.Uid;
            int ProductId = 0;
            int Count = 0;
            int.TryParse(productId, out ProductId);
            int.TryParse(count,out Count);
            _shopCarAccess.UpdateCar(ProductId, Count, property, uid, (int)ShopCarType.ShopCar);
            return Content("");
        }

        /// <summary>
        /// 即将生成的订单的预览页面
        /// </summary>
        /// <param name="productId">
        /// 产品Id
        /// 订单有两种方式
        /// 1.productId=0表示通过购物车生成订单
        /// 2.productId!=0表示生成该产品Id的订单
        /// </param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public ActionResult OrderPreview([DefaultValue(0)]int productId, [DefaultValue(0)]int pageIndex)
        {
            this.SiteMapPathAdd(ViewsResources.SiteMapPath.OrderPreview);
            int pageCount = 0;
            OrderViewModel _viewModel = new OrderViewModel();
            _viewModel = _orderAccess.OrderPreivew(_user.Uid, productId, pageCount);
            _viewModel.ExpressList = _orderAccess.ExpressList();
            return View(_viewModel);
        }

        /// <summary>
        /// 订单列表
        /// </summary>
        public ActionResult OrderList([DefaultValue(0)]int pageIndex)
        {
            this.SiteMapPathAdd(ViewsResources.SiteMapPath.OrderList);
            int pageCount = 0;
            OrderViewModel _viewModel = new OrderViewModel();
            _viewModel.OrderList = _orderAccess.SaleList(_user.Uid, pageIndex, ref pageCount);
            ViewData["Pager"] = this.CreatePager(Url.Action("OrderList", new { pageIndex = "{0}" }), pageCount, pageIndex);
            return View(_viewModel);
        }
        /// <summary>
        /// 订单预览
        /// </summary>
        public ActionResult OrderView([DefaultValue("")]string orderId, [DefaultValue(0)]int pageIndex)
        {
            this.SiteMapPathAdd(ViewsResources.SiteMapPath.OrderList, Url.Action("OrderList", new { pageIndex = pageIndex }));
            this.SiteMapPathAdd(ViewsResources.SiteMapPath.OrderView);

            OrderViewModel orderView = _orderAccess.SaleOrderView(_user.Uid, orderId);
            if (orderView == null)
                return Content("订单没有找到!!");
            return View(orderView);
        }
        /// <summary>
        /// 财务记录列表
        /// </summary>
        public ActionResult Bill()
        {
            this.SiteMapPathAdd(ViewsResources.SiteMapPath.Bill);

            return View();
        }
    }
}
